﻿using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class NPS_app_MasterData_ReasonCategoryMaster : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrTitle.Text = "Master Data: Reason Category";
            loadView();
            setPanel();
            pnlView.Visible = true;
        }
        else
        {
            if (pnlView.Visible)
            {
                loadView();
            }
        }
    }


    private void setPanel()
    {
        pnlView.Visible = false;
        pnlCreate.Visible = false;
        pnlEdit.Visible = false;
        pnlDetails.Visible = false;
        pnlDelete.Visible = false;
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        loadData(e.CommandArgs.CommandArgument.ToString());
        if (e.CommandArgs.CommandName == "Editing")
        {
            goEdit();
        }
        else if (e.CommandArgs.CommandName == "Details")
        {
            goDetail();
        }
        else if (e.CommandArgs.CommandName == "Deleting")
        {
            goDelete();
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        goNew();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        loadView();
    }
    protected void btnCSave_Click(object sender, EventArgs e)
    {
        Entities.NPSReasonCategory marketingName = new Entities.NPSReasonCategory()
        {
            sxCreator = ICPUser.ID,
            sxTitle = txtCTitle.Text,
            sxDetail = txtCDetail.Text
        };
        saveReasonCategory(marketingName);
    }
    protected void btnCCancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnESave_Click(object sender, EventArgs e)
    {
        Entities.NPSReasonCategory marketingName = new Entities.NPSReasonCategory()
        {
            iID = getID,
            sxUpdater = ICPUser.ID,
            sxTitle = txtETitle.Text,
            sxDetail = txtEDetail.Text
        };
        updateReasonCategory(marketingName);
    }
    protected void btnECancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDEdit_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDBack_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Entities.NPSReasonCategory marketingName = new Entities.NPSReasonCategory()
        {
            iID = getID,
            sxCreator = ICPUser.ID
        };
        deleteReasonCategory(marketingName);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        goView();
    }

    #region Audy's Custom Function

    protected int getID
    {
        get { return Convert.ToInt32(hfID.Value); }
    }
    protected void goView()
    {
        setPanel();
        pnlView.Visible = true;
        ltrTitle.Text = "Master Data Reason Category";
        txtCDetail.Text = string.Empty;
        txtCTitle.Text = string.Empty;
    }

    protected void goEdit()
    {
        setPanel();
        pnlEdit.Visible = true;
        ltrTitle.Text = "Master Data Reason Category Edit";
    }

    protected void goNew()
    {
        setPanel();
        pnlCreate.Visible = true;
        ltrTitle.Text = "Master Data Reason Category Create";
    }

    protected void goDetail()
    {
        setPanel();
        pnlDetails.Visible = true;
        ltrTitle.Text = "Master Data Reason Category Details";
    }

    protected void goDelete()
    {
        setPanel();
        pnlDelete.Visible = true;
        ltrTitle.Text = "Master Data Reason Category Delete";
    }

    protected void loadView()
    {
        Controller.NPSReasonCategoryController mnc = new Controller.NPSReasonCategoryController();
        GridView1.DataSource = mnc.NPSReasonCategoryList;
        GridView1.DataBind();
    }
    protected void loadData(string id)
    {
        //fetch data from DB
        Controller.NPSReasonCategoryController ANController = new Controller.NPSReasonCategoryController();
        Entities.NPSReasonCategory ReasonCategory = ANController.getNPSReasonCategory(Convert.ToInt32(id));

        //set all data to forms
        txtETitle.Text = ReasonCategory.sxTitle;
        lblDT.Text = ReasonCategory.sxTitle;
        lblT.Text = ReasonCategory.sxTitle;

        txtEDetail.Text = ReasonCategory.sxDetail;
        lblDD.Text = ReasonCategory.sxDetail;
        lblD.Text = ReasonCategory.sxDetail;

        lblID.Text = ReasonCategory.iID.ToString();
        lblDID.Text = ReasonCategory.iID.ToString();
        hfID.Value = ReasonCategory.iID.ToString();
    }
    protected void saveReasonCategory(Entities.NPSReasonCategory ReasonCategory)
    {
        ReasonCategory.sxCreator = ICPUser.ID;
        Controller.NPSReasonCategoryController ANController = new Controller.NPSReasonCategoryController();
        if (ANController.InsertNPSReasonCategoryData(ReasonCategory) == 0)
        { //return false; 
        }
        else
        { //return true;
            loadView();
        }
        goView();
        AlertNotification("Kategori alasan " + ReasonCategory.sxTitle + " telah tersimpan");
    }

    protected void updateReasonCategory(Entities.NPSReasonCategory ReasonCategory)
    {
        Controller.NPSReasonCategoryController ANController = new Controller.NPSReasonCategoryController();
        ANController.UpdateNPSReasonCategoryData(ReasonCategory);
        loadView();
        goView();
        AlertNotification("Kategori alasan " + ReasonCategory.sxTitle + " telah diperbaharui");
    }

    protected void deleteReasonCategory(Entities.NPSReasonCategory ReasonCategory)
    {
        Controller.NPSReasonCategoryController ANController = new Controller.NPSReasonCategoryController();
        ANController.DeleteNPSReasonCategoryData(ReasonCategory);
        loadView();
        goView();
        AlertNotification("Kategori alasan " + ReasonCategory.sxTitle + " telah dihapus");
    }
    #endregion
}
